<template>
  <div class="passenger">
    <a-card title="余票信息">
      <a-form layout="inline" :model="formObj">
        <a-form-item>
          <a-date-picker
            v-model:value="formObj.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item>
          <TrainSelect v-model:code="formObj.trainCode" width="200px" />
        </a-form-item>
        <a-form-item>
          <StationSelect v-model:name="formObj.start" width="200px" placeholder="请选择出发站" />
        </a-form-item>
        <a-form-item>
          <StationSelect v-model:name="formObj.end" width="200px" placeholder="请选择到达站" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="getList">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table :data-source="dataList" :columns="columns" :pagination="false">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'operation'"></template>
        <temaplate v-else-if="column.dataIndex === 'station'">
          {{ record.start }}
          <br />
          {{ record.end }}
        </temaplate>
        <temaplate v-else-if="column.dataIndex === 'time'">
          {{ record.startTime }}
          <br />
          {{ record.endTime }}
        </temaplate>
        <temaplate v-else-if="column.dataIndex === 'duration'">
          {{ calDaration(record.startTime, record.endTime) }}
          <br />
          <div v-if="record.startTime.replaceAll(':', '') > record.endTime.replaceAll(':', '')">
            次日到达
          </div>
          <div v-else>当日到达</div>
        </temaplate>
        <template v-else-if="column.dataIndex === 'ydz'">
          <div v-if="record.ydz >= 0">
            {{ record.ydz }}
            <br />
            {{ record.ydzPrice }}￥
          </div>
          <div v-else>--</div>
        </template>
        <template v-else-if="column.dataIndex === 'edz'">
          <div v-if="record.edz >= 0">
            {{ record.edz }}
            <br />
            {{ record.edzPrice }}￥
          </div>
          <div v-else>--</div>
        </template>
        <template v-else-if="column.dataIndex === 'rw'">
          <div v-if="record.rw >= 0">
            {{ record.rw }}
            <br />
            {{ record.rwPrice }}￥
          </div>
          <div v-else>--</div>
        </template>
        <template v-else-if="column.dataIndex === 'yw'">
          <div v-if="record.yw >= 0">
            {{ record.yw }}
            <br />
            {{ record.ywPrice }}￥
          </div>
          <div v-else>--</div>
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @change="onChange"
    />
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { DailyTrainTicketApi } from '../../api/dailyTrainTicket';
import TrainSelect from '@/components/Train-Select.vue';
import StationSelect from '../../components/Station-Select.vue';
import dayjs from 'dayjs';

const columns = ref([
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
    width: 80,
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
    width: 110,
  },
  {
    title: '车站',
    dataIndex: 'station',
    key: 'station',
  },
  {
    title: '时间',
    dataIndex: 'time',
    key: 'time',
  },
  {
    title: '历时',
    dataIndex: 'duration',
    key: 'duration',
  },
  {
    title: '出发站序',
    dataIndex: 'startIndex',
    key: 'startIndex',
  },
  {
    title: '到站站序',
    dataIndex: 'endIndex',
    key: 'endIndex',
  },
  {
    title: '一等座',
    dataIndex: 'ydz',
    key: 'ydz',
  },
  {
    title: '二等座',
    dataIndex: 'edz',
    key: 'edz',
  },
  {
    title: '软卧',
    dataIndex: 'rw',
    key: 'rw',
  },
  {
    title: '硬卧',
    dataIndex: 'yw',
    key: 'yw',
  },
]);

const formObj = ref({
  date: null,
  trainCode: '',
  start: '',
  end: '',
  page: 1,
  pageSize: 10,
  total: 0,
});

onMounted(() => {
  getList();
});

const dataList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await DailyTrainTicketApi.query(formObj.value);
  dataList.value = data.records || [];
  formObj.value.total = data.total;
};

const reset = (): void => {
  formObj.value.trainCode = '';
  formObj.value.start = '';
  formObj.value.end = '';
  formObj.value.date = null;
  getList();
};

const onChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};

const calDaration = (startTime: string, endTime: string): any => {
  let diff = dayjs(endTime, 'HH:mm:ss').diff(dayjs(startTime, 'HH:mm:ss'), 'seconds');
  return dayjs('00:00:00', 'HH:mm:ss').second(diff).format('HH:mm:ss');
};
</script>

<style scoped lang="less">
.passenger {
  :global(.ant-modal .ant-modal-footer) {
    text-align: center;
  }
}
</style>
